var tls = require('tls'),
    sys = require('sys'),
    fs = require('fs');

exports.listen = function(port)
{
    var options = {
        key: fs.readFileSync('server.key'),
        cert: fs.readFileSync('cert.pem'),
        port: port
    };

    sys.puts("Tls server peer started.");

    tls.createServer(options, function (socket)
    {
        sys.puts("Tls connection established from: " + socket.remoteAddress);

        socket.addListener("data", function (data)
        {        
            fs.appendFile('files' + port, data, 'utf8', function (err, data)
    		{	  
    			if (err)
    				return console.log(err);
    		});
        });

       socket.pipe(socket);

    }).listen(port);
};